// state:存储的状态信息，比如用户信息
// getters 简单获取
// mutation(尽量同步)：定义了对变量进行增删改（更新）的方法
// actions(支持异步)：执行异步操作，并且触发mutation的更改(actions调用mutation)
// modules(模块)：把一个大的state(全局变量)划分为多个小模块，比如user专门存用户的状态信息

import { StoreOptions } from "vuex";
import ACCESS_ENUM from "@/access/acessEnum";
import { UserControllerService } from "../../generated";

export default {
  namespaced: true,
  state: () => ({
    loginUser: {
      userName: "未登录",
    },
  }),
  actions: {
    async getLoginUser({ commit, state }) {
      // 从远程请求获取登录信息
      const res = await UserControllerService.getLoginUserUsingGet();
      if (res.code === 0) {
        commit("updateUser", res.data);
      } else {
        commit("updateUser", {
          ...state.loginUser,
          userRole: ACCESS_ENUM.NOT_LOGIN,
        });
      }
    },
  },
  mutations: {
    updateUser(state, payload) {
      state.loginUser = payload;
    },
  },
} as StoreOptions<any>;
